<html>
<head><meta charset="utf-8"><title>Can I rely on pointer escape/capture ? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html">Can I rely on pointer escape/capture ?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="203771634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203771634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203771634">(Jul 13 2020 at 21:08)</a>:</h4>
<p>Hello, while doing some API to access a DMA peripheral, I noticed a theoretical problem that could be caused by constant propagation, in my case it's solved by a pointer escape, but that doesn't seem to be a guarantee that the language gives.</p>
<p>See some code examples here:<br>
<a href="https://godbolt.org/z/7Y96Ks">https://godbolt.org/z/7Y96Ks</a></p>
<p>If I can't rely on that, how can we rely on FFI to work ? That seems to be the same thing that would avoid constant propagation through FFI boundaries.</p>



<a name="203805290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203805290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203805290">(Jul 14 2020 at 07:21)</a>:</h4>
<p><span class="user-mention" data-user-id="267734">@Thales Fragoso</span> could you explain what happens there to someone who cannot read assembly?</p>



<a name="203805349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203805349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203805349">(Jul 14 2020 at 07:22)</a>:</h4>
<p>I see that uncommenting the <code>write_volatile</code> line changes stuff on the right but I cannot really tell what that means</p>



<a name="203806491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203806491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203806491">(Jul 14 2020 at 07:43)</a>:</h4>
<p>looks like the <code>write_volatile</code> is being reordered before <code>Descriptor</code> is stored to the stack</p>



<a name="203806514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203806514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203806514">(Jul 14 2020 at 07:44)</a>:</h4>
<p><span class="user-mention" data-user-id="267734">@Thales Fragoso</span> this is the same in C: see <a href="https://gcc.gnu.org/onlinedocs/gcc/Volatiles.html">https://gcc.gnu.org/onlinedocs/gcc/Volatiles.html</a> "Accesses to non-volatile objects are not ordered with respect to volatile accesses. "</p>



<a name="203806603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203806603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203806603">(Jul 14 2020 at 07:45)</a>:</h4>
<p><span class="user-mention" data-user-id="267734">@Thales Fragoso</span> you probably want a <code>compiler_fence</code>: <a href="https://doc.rust-lang.org/nightly/std/sync/atomic/fn.compiler_fence.html">https://doc.rust-lang.org/nightly/std/sync/atomic/fn.compiler_fence.html</a></p>



<a name="203806791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203806791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203806791">(Jul 14 2020 at 07:49)</a>:</h4>
<p>at least, that will prevent the optimization in question.  however, depending on your target device, you may actually need a hardware memory barrier instruction (<code>dmb</code>) ...</p>



<a name="203807048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203807048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203807048">(Jul 14 2020 at 07:53)</a>:</h4>
<p>in that case, you can use this: <a href="https://doc.rust-lang.org/nightly/core/arch/arm/fn.__dmb.html">https://doc.rust-lang.org/nightly/core/arch/arm/fn.__dmb.html</a></p>



<a name="203807202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203807202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203807202">(Jul 14 2020 at 07:55)</a>:</h4>
<p>actually, on some devices you may even need to flush the cache; it depends</p>



<a name="203807300"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203807300" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203807300">(Jul 14 2020 at 07:57)</a>:</h4>
<p>yeah when doing emssage-passing between different threads / devices, I'd always expect the need for some kind of fence</p>



<a name="203807380"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203807380" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203807380">(Jul 14 2020 at 07:58)</a>:</h4>
<p>there should probably be a warning about this in the documentation for <code>write_volatile</code></p>



<a name="203807409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203807409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203807409">(Jul 14 2020 at 07:58)</a>:</h4>
<p>that warning should probably be written by someone who knows volatile stuff better than I do :)</p>



<a name="203807436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203807436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203807436">(Jul 14 2020 at 07:59)</a>:</h4>
<p>do you want to prepare a PR? I can help reviewing</p>



<a name="203807566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203807566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203807566">(Jul 14 2020 at 08:00)</a>:</h4>
<p>sure, why not</p>



<a name="203807609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203807609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203807609">(Jul 14 2020 at 08:00)</a>:</h4>
<p>I'll do it tomorrow</p>



<a name="203807680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203807680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203807680">(Jul 14 2020 at 08:01)</a>:</h4>
<p>thanks :)</p>



<a name="203808004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203808004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203808004">(Jul 14 2020 at 08:05)</a>:</h4>
<p>(I still think the documentation shouldn't claim that data races with volatile accesses are undefined behavior, but IIRC that discussion ended with some vague plan to replace volatile with architecture-specific intrinsics, which never happened…)</p>



<a name="203808167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203808167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203808167">(Jul 14 2020 at 08:07)</a>:</h4>
<p>well, it should still happen, if only as a way to deprecate volatile accesses of struct types, which are totally unspecified and useless</p>



<a name="203816031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203816031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203816031">(Jul 14 2020 at 09:58)</a>:</h4>
<p><span class="user-mention silent" data-user-id="198590">comex</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F/near/203808004">said</a>:</p>
<blockquote>
<p>(I still think the documentation shouldn't claim that data races with volatile accesses are undefined behavior, but IIRC that discussion ended with some vague plan to replace volatile with architecture-specific intrinsics, which never happened…)</p>
</blockquote>
<p>In C/C++ they are UB, and we haven't been able to convince the LLVM devs to document anything stronger. So I think our hands are tied here.</p>



<a name="203816083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203816083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203816083">(Jul 14 2020 at 09:59)</a>:</h4>
<p>We specifically asked them if they could commit to a volatile load not ever returning <code>undef</code> (a sensible guarantee IMO and also taking care of read-write races the LLVM way), and they said no.</p>



<a name="203816205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203816205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203816205">(Jul 14 2020 at 10:01)</a>:</h4>
<p>(see <a href="https://bugs.llvm.org/show_bug.cgi?id=42435">https://bugs.llvm.org/show_bug.cgi?id=42435</a>)</p>



<a name="203819817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203819817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203819817">(Jul 14 2020 at 10:54)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> what I'm trying to do is prevent the compiler compiler from eliding the load in the <code>let b =</code></p>



<a name="203819974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203819974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203819974">(Jul 14 2020 at 10:56)</a>:</h4>
<p>Because the DMA changed the buffer but the compiler doesn't know, but when a pointer to that buffer escapes (plus fence) then it won't elide the <code>ldr</code></p>



<a name="203820138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203820138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203820138">(Jul 14 2020 at 10:58)</a>:</h4>
<p><span class="user-mention" data-user-id="198590">@comex</span> I don't need that particular <code>write_volatile</code> to be before pushing the values of the descriptor onto the stack, I just needed it to be in place just before enabling the DMA, see the fences in <code>start_and_wait_dma</code></p>



<a name="203820191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203820191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203820191">(Jul 14 2020 at 10:59)</a>:</h4>
<p>I'm aware of reordering non volatile operations w.r.t volatiles and in this particular case I don't need a dmb, because cortex-m3</p>



<a name="203820328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203820328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203820328">(Jul 14 2020 at 11:01)</a>:</h4>
<p>Also, <code>atomic::fence</code> (not compiler_fence) does generate a <code>dmb</code> so there is no need to use the intrinsics  directly</p>



<a name="203820567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203820567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203820567">(Jul 14 2020 at 11:04)</a>:</h4>
<p>And I also agree that data races (even with volatiles) are UB</p>



<a name="203820868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203820868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203820868">(Jul 14 2020 at 11:09)</a>:</h4>
<p>I dont know much about volatile, but with atomics you definitely have to put the fence between the non-atomic write and the release write</p>



<a name="203820887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203820887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203820887">(Jul 14 2020 at 11:09)</a>:</h4>
<p>so here that would be before the <code>write_volatile</code></p>



<a name="203820911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203820911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203820911">(Jul 14 2020 at 11:09)</a>:</h4>
<p>otherwise whoever reads from that <code>write_volatile</code> is not guaranteed to actually see your data writes</p>



<a name="203820987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203820987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203820987">(Jul 14 2020 at 11:10)</a>:</h4>
<p>IOW, the other side <em>could</em> do the read between the <code>write_volatile</code> and the fence</p>



<a name="203820988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203820988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203820988">(Jul 14 2020 at 11:10)</a>:</h4>
<p>It won't read before I enable it in dma_start, which has a fence before enabling</p>



<a name="203821018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821018">(Jul 14 2020 at 11:10)</a>:</h4>
<p>there's nothing happening in <code>dma_start_and_wait</code> other than fences?</p>



<a name="203821030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821030">(Jul 14 2020 at 11:11)</a>:</h4>
<p>so there's no way for the compiler to know that there is any communication there</p>



<a name="203821037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821037">(Jul 14 2020 at 11:11)</a>:</h4>
<p>Heh, the comments should be changed to the correct registers accesses</p>



<a name="203821044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821044">(Jul 14 2020 at 11:11)</a>:</h4>
<p>It's just an example</p>



<a name="203821054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821054">(Jul 14 2020 at 11:11)</a>:</h4>
<p>a fence on its own doesnt sync, only fences appropriately "paired with" accesses do</p>



<a name="203821129"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821129" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821129">(Jul 14 2020 at 11:12)</a>:</h4>
<p>I know, there will be an access there, but in this case it isn't the thing I'm focussing</p>



<a name="203821140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821140">(Jul 14 2020 at 11:12)</a>:</h4>
<p>not sure if that makes a difference, but the code you posted there is just UB if <code>dma_buffer</code> is mutated</p>



<a name="203821167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821167">(Jul 14 2020 at 11:12)</a>:</h4>
<p>Hmm, it's mutated by the DMA, why the UB ?</p>



<a name="203821189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821189">(Jul 14 2020 at 11:13)</a>:</h4>
<p>well again I can only speak for release/acquire concurrency, which I actually understand, unlike volatile semantics</p>



<a name="203821220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821220">(Jul 14 2020 at 11:13)</a>:</h4>
<p>a release fence doesnt do anything, a release fence followed by a relaxed write does. there is no atomic access after the release fence so the fence is a NOP.</p>



<a name="203821221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821221">(Jul 14 2020 at 11:14)</a>:</h4>
<p>All the mutation will happen in that start_and_wait</p>



<a name="203821268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821268">(Jul 14 2020 at 11:14)</a>:</h4>
<p>but it doesnt, and the compiler is allowed to inline that function and see that it just has 2 fences</p>



<a name="203821284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821284">(Jul 14 2020 at 11:14)</a>:</h4>
<p>fences that have no chance of "pairing up" with anything</p>



<a name="203821289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821289">(Jul 14 2020 at 11:14)</a>:</h4>
<p>and thus are inconsequential</p>



<a name="203821306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821306">(Jul 14 2020 at 11:15)</a>:</h4>
<p>Sorry, I should have put the write (volatile) to the dma enabling register there</p>



<a name="203821391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821391">(Jul 14 2020 at 11:16)</a>:</h4>
<p>But it this case it doesn't make a difference because why I trying to exemplify is the eliding or not eliding of the ldr in <code>let b =</code></p>



<a name="203821403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821403">(Jul 14 2020 at 11:16)</a>:</h4>
<p>sure but the code between the "release write" and the <code>let</code> matters for that</p>



<a name="203821412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821412">(Jul 14 2020 at 11:16)</a>:</h4>
<p>every bit of code there can make a huge difference</p>



<a name="203821432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821432">(Jul 14 2020 at 11:17)</a>:</h4>
<p>not sure why you say it wouldnt^^ maybe the compiler isnt smart enough, but that's all</p>



<a name="203821479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821479">(Jul 14 2020 at 11:17)</a>:</h4>
<p>it is suspicious though that the double-indirection makes a difference, this makes it smell like an LLVM bug to me. but again volatile semantics are tricky... this would be easier for me to analyze if we replaced them by atomics.</p>



<a name="203821542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821542">(Jul 14 2020 at 11:18)</a>:</h4>
<p>Hmm, I on the phone now and godbolt doesn't play along with it, but you can change those two comments in dma_start_wait to</p>
<div class="codehilite"><pre><span></span><code>write_volatile(0x40000004, 1);
while read_volatile(0x40000008) == 1 {}
</code></pre></div>



<a name="203821573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821573">(Jul 14 2020 at 11:19)</a>:</h4>
<p>I don't think the dissasemble will change because of that, I mean, it will do the write and read, but the ldr thing will remain the same</p>



<a name="203821586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821586">(Jul 14 2020 at 11:19)</a>:</h4>
<p>I assume these are <code>u32</code> pointers?</p>



<a name="203821654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821654">(Jul 14 2020 at 11:20)</a>:</h4>
<p>so much code, no idea what to look for^^ <a href="https://godbolt.org/z/rP1KeK">https://godbolt.org/z/rP1KeK</a></p>



<a name="203821678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821678">(Jul 14 2020 at 11:20)</a>:</h4>
<p>Oh yeah, sorry, forgot the as *mut u32</p>



<a name="203821687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821687">(Jul 14 2020 at 11:20)</a>:</h4>
<p>and I mean, <code>dma_with_descriptors</code> does have an <code>ldr</code> in your original code (after adding the volatile write), so I am not sure I even understand the problem</p>



<a name="203821711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821711">(Jul 14 2020 at 11:21)</a>:</h4>
<p>You mean uncommenting the volatile write ?</p>



<a name="203821719"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821719" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821719">(Jul 14 2020 at 11:21)</a>:</h4>
<p>yeah</p>



<a name="203821733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821733">(Jul 14 2020 at 11:21)</a>:</h4>
<p>that's your actual code, right? not sure why it is commented in your snippet</p>



<a name="203821749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821749">(Jul 14 2020 at 11:21)</a>:</h4>
<p>when it is commented of course the compiler optimizes the heck out of this^^</p>



<a name="203821752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821752">(Jul 14 2020 at 11:21)</a>:</h4>
<p>This is what I'm saying, what happens in that volatile write is that the pointer escapes</p>



<a name="203821798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821798">(Jul 14 2020 at 11:22)</a>:</h4>
<p>What I want to know is if rust considers this a thing</p>



<a name="203821809"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821809" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821809">(Jul 14 2020 at 11:22)</a>:</h4>
<p>no it doesn't</p>



<a name="203821841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821841">(Jul 14 2020 at 11:23)</a>:</h4>
<p>but writing a pointer to memory is a legal way to communicate that memory to another thread (when synchrinization is done properly)</p>



<a name="203821857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821857">(Jul 14 2020 at 11:23)</a>:</h4>
<p><a href="https://jonasdevlieghere.com/escape-analysis-capture-tracking-in-llvm/">https://jonasdevlieghere.com/escape-analysis-capture-tracking-in-llvm/</a></p>



<a name="203821860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821860">(Jul 14 2020 at 11:23)</a>:</h4>
<p>the "pointer escaping" thing is just a consequence of the spec, not a part of the spec</p>



<a name="203821935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821935">(Jul 14 2020 at 11:24)</a>:</h4>
<p>I thought you said that in <code>dma_with_descriptors</code> it didnt work properly, incorrectly eliding the load, because the ptr was not escaped? did I misunderstand?</p>



<a name="203821960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821960">(Jul 14 2020 at 11:24)</a>:</h4>
<p>keep in mind that I cannot read assembly, so as such your problem statement is still not clear to me^^</p>



<a name="203821983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203821983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203821983">(Jul 14 2020 at 11:24)</a>:</h4>
<p>just that it is about eliding the load under some circumstances, not sure which</p>



<a name="203822003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822003">(Jul 14 2020 at 11:25)</a>:</h4>
<p>It does escape if you uncomment the write_volatile, I just left it commented so people could see the escape in action on the assembly heh</p>



<a name="203822027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822027">(Jul 14 2020 at 11:25)</a>:</h4>
<p>but then what is the bug? is there no bug? I am confused^^</p>



<a name="203822049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822049">(Jul 14 2020 at 11:25)</a>:</h4>
<p>I thought the compiler didnt do what you wanted it to do unless you explicitly escaped the pointer, or so</p>



<a name="203822095"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822095" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822095">(Jul 14 2020 at 11:26)</a>:</h4>
<p>There is no bug, hah, I wanted to know (vide the name of the topic) if I can rely on that</p>



<a name="203822104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822104">(Jul 14 2020 at 11:26)</a>:</h4>
<p>but it looks like the compiler actually does what you want it do do, you just want to know why?</p>



<a name="203822132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822132">(Jul 14 2020 at 11:26)</a>:</h4>
<p>I wanted to know this</p>
<blockquote>
<p>but writing a pointer to memory is a legal way to communicate that memory to another thread (when synchrinization is done properly)</p>
</blockquote>



<a name="203822140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822140">(Jul 14 2020 at 11:26)</a>:</h4>
<p>well so "pointer escape" is not part of the spec so not <em>directly</em> something you can rely on</p>



<a name="203822161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822161">(Jul 14 2020 at 11:27)</a>:</h4>
<p>That what you wrote is my understanding of pointer escape</p>



<a name="203822174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822174">(Jul 14 2020 at 11:27)</a>:</h4>
<p>I.e. some other thread might have it</p>



<a name="203822197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822197">(Jul 14 2020 at 11:28)</a>:</h4>
<p>but the compiler may only perform transformations that <em>do not change program behavior</em>, and crucially (since we are only seeing part of the program), behavior must not change <em>with any possible "context"</em>, where "context" means "the rest of the program/system", i.e. all the other code this interacts with</p>



<a name="203822270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822270">(Jul 14 2020 at 11:28)</a>:</h4>
<p>so if there is another thread that you can run in parallel to this one, and if that thread could in a UB-free way obtain the <code>dma_buffer</code> address and change its contents before it gets read again by the main thread, then the possibility of that thread existing is something the compiler has to take into account</p>



<a name="203822279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822279">(Jul 14 2020 at 11:29)</a>:</h4>
<p>and from that one can then derive a notion of "escaped pointer"</p>



<a name="203822394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822394">(Jul 14 2020 at 11:30)</a>:</h4>
<p>So the language/compiler has the notion of an escaped pointer, right ?</p>



<a name="203822405"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822405" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822405">(Jul 14 2020 at 11:30)</a>:</h4>
<p>I think your original code actually didnt make this possible in a ub-free way; the compiler considered the ptr "escaped" but that was a conservative analysis and a smarter compiler could have elided the load</p>



<a name="203822421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822421">(Jul 14 2020 at 11:30)</a>:</h4>
<p>without the loop waiting for "DMA completed", if the other thread did a write, there would be a data race when you read <code>dma_buffer</code></p>



<a name="203822454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822454">(Jul 14 2020 at 11:31)</a>:</h4>
<p>so in that sense "the ptr escaped" is not all you need to consider here</p>



<a name="203822459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822459">(Jul 14 2020 at 11:31)</a>:</h4>
<p>even if the ptr escaped, UB-free mutation could be impossible</p>



<a name="203822526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822526">(Jul 14 2020 at 11:32)</a>:</h4>
<p>Yeah, I should have put the code in the dma function, I just didn't because it didnt make a difference w.r.t. code generation of what I was focussing</p>



<a name="203822559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822559">(Jul 14 2020 at 11:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="267734">Thales Fragoso</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F/near/203822394">said</a>:</p>
<blockquote>
<p>So the language/compiler has the notion of an escaped pointer, right ?</p>
</blockquote>
<p>I think it is possible to define such a notion, but I am not familiar with how. more interesting though is the question of what the notion is useful for.</p>



<a name="203822576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822576">(Jul 14 2020 at 11:32)</a>:</h4>
<p>"the ptr did not escape" is a useful notion as it certainly makes ub-free communication impossible</p>



<a name="203822586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822586">(Jul 14 2020 at 11:33)</a>:</h4>
<p>but "the ptr escaped" doesnt imply anything useful, as far as I can tell</p>



<a name="203822677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822677">(Jul 14 2020 at 11:34)</a>:</h4>
<p>and your original code IMO is an example for that -- the ptr did escape but the load could still have been elided as there was no synchronization for communicating the mutation back to your code</p>



<a name="203822722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822722">(Jul 14 2020 at 11:34)</a>:</h4>
<p>ultimately the only thing you can rely on is that the compiler will not change program behavior when your program is run together with any possible context <em>in a ub-free way</em></p>



<a name="203822739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822739">(Jul 14 2020 at 11:35)</a>:</h4>
<p>Well, the acquire should do the synchronization</p>



<a name="203822748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822748">(Jul 14 2020 at 11:35)</a>:</h4>
<p>no an acquire fence alone doesnt do anything</p>



<a name="203822757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822757">(Jul 14 2020 at 11:35)</a>:</h4>
<p>only a relaxed (or stronger) load followed by an acquire fence does anything</p>



<a name="203822823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822823">(Jul 14 2020 at 11:36)</a>:</h4>
<p>Well, there is a load before the fence, but It should be in the function, I know</p>



<a name="203822824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822824">(Jul 14 2020 at 11:36)</a>:</h4>
<p>I know it is very common to think of fences as "blocking reordering", but that is not their specification</p>



<a name="203822858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822858">(Jul 14 2020 at 11:36)</a>:</h4>
<p><span class="user-mention silent" data-user-id="267734">Thales Fragoso</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F/near/203822823">said</a>:</p>
<blockquote>
<p>Well, there is a load before the fence, but I should be in the function, I know</p>
</blockquote>
<p>oh you mean with the <code>read_volatile</code> loop? then yes</p>



<a name="203822860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822860">(Jul 14 2020 at 11:37)</a>:</h4>
<p>I was referring to your original code without that loop</p>



<a name="203822861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822861">(Jul 14 2020 at 11:37)</a>:</h4>
<p>For now on, could you consider that the code I pasted up there is in the godbolt code ?</p>



<a name="203822875"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822875" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822875">(Jul 14 2020 at 11:37)</a>:</h4>
<p>Do you still see an UB with that ?</p>



<a name="203822882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822882">(Jul 14 2020 at 11:37)</a>:</h4>
<p>yes the final code with the <code>0x40000004</code>/<code>0x40000008</code> accesses is fine</p>



<a name="203822887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822887">(Jul 14 2020 at 11:37)</a>:</h4>
<p>as that enables ub-free communication both ways</p>



<a name="203822953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822953">(Jul 14 2020 at 11:38)</a>:</h4>
<p>(it is fine for atomics; volatile is weird but hopefully works similarly enough^^)</p>



<a name="203822980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203822980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203822980">(Jul 14 2020 at 11:39)</a>:</h4>
<p>And I can rely that the compiler won't optimize the <code>ldr</code> because I wrote the  pointer to a place in memory that other thread might have access to (escaped ptr), right ?</p>



<a name="203823079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203823079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203823079">(Jul 14 2020 at 11:40)</a>:</h4>
<p>(Together with the synchronization to enable UB-free concurrency)</p>



<a name="203823223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203823223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203823223">(Jul 14 2020 at 11:42)</a>:</h4>
<p><span class="user-mention silent" data-user-id="267734">Thales Fragoso</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F/near/203822980">said</a>:</p>
<blockquote>
<p>And I can rely that the compiler won't optimize the <code>ldr</code> because I wrote the  pointer to a place in memory that other thread might have access to (escaped ptr), right ?</p>
</blockquote>
<p>might have access to <em>and might legally mutate</em> (this is where the <code>while volatile_read</code> becomes crucial)</p>



<a name="203823230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203823230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203823230">(Jul 14 2020 at 11:42)</a>:</h4>
<p>yes</p>



<a name="203823265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203823265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203823265">(Jul 14 2020 at 11:42)</a>:</h4>
<p>Great, thanks Ralf</p>



<a name="203844331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203844331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203844331">(Jul 14 2020 at 15:05)</a>:</h4>
<p>At some point i might also have to step through the construction of my own DMA routine XD</p>



<a name="203844384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203844384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203844384">(Jul 14 2020 at 15:05)</a>:</h4>
<p>this long thread makes me nervous about it for sure</p>



<a name="203844556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203844556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203844556">(Jul 14 2020 at 15:06)</a>:</h4>
<p>There are some good points about it on the embedonomicon, although there is a part that is wrong</p>



<a name="203844579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203844579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203844579">(Jul 14 2020 at 15:07)</a>:</h4>
<p>When it talks about Pin</p>



<a name="203844666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203844666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203844666">(Jul 14 2020 at 15:07)</a>:</h4>
<p>See <a href="https://github.com/rust-embedded/embedonomicon/issues/64">https://github.com/rust-embedded/embedonomicon/issues/64</a></p>



<a name="203846573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203846573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203846573">(Jul 14 2020 at 15:21)</a>:</h4>
<p>in my case the DMA generally overrides the CPU, not runs alongside it, so it becomes much less complicated</p>



<a name="203865971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203865971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203865971">(Jul 14 2020 at 17:59)</a>:</h4>
<p>You need to think of the DMA engine as a separate thread. The pointer is not escaping, instead you are sending the pointer to the DMA engine thread. This means that you need a release fence between filling your buffer and sending a pointer to it to the DMA engine. Similarly you need an acquire fence after receiving a completion from the DMA engine before you can reuse the buffer or read back data from the buffer.</p>



<a name="203866624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203866624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203866624">(Jul 14 2020 at 18:05)</a>:</h4>
<p>what if the system doesn't have atomic operations</p>



<a name="203866963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203866963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203866963">(Jul 14 2020 at 18:08)</a>:</h4>
<p>further, what if the CPU is halted while the DMA runs (which i know is uncommon,  but that is how it works in my case)</p>



<a name="203867049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203867049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203867049">(Jul 14 2020 at 18:09)</a>:</h4>
<p>You still need a compiler fence at the very least.</p>



<a name="203867078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203867078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203867078">(Jul 14 2020 at 18:09)</a>:</h4>
<p>fair</p>



<a name="203867148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203867148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203867148">(Jul 14 2020 at 18:10)</a>:</h4>
<p>Whether an actual fence instruction is needed is hardware dependent.</p>



<a name="203867280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203867280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203867280">(Jul 14 2020 at 18:11)</a>:</h4>
<p>I do not believe that ARMv4T has fencing instructions when there is no co-processor</p>



<a name="203867561"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203867561" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203867561">(Jul 14 2020 at 18:13)</a>:</h4>
<p>It doesn't. Native fence instructions were introduced in ARMv6, before that they were available through CP15.</p>



<a name="203870862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203870862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203870862">(Jul 14 2020 at 18:40)</a>:</h4>
<blockquote>
<p>This means that you need a release fence between filling your buffer and sending a pointer to it to the DMA engine.</p>
</blockquote>
<p><span class="user-mention" data-user-id="143274">@Amanieu</span> <br>
Can the release fence be before starting the DMA engine but after sending the address ?</p>



<a name="203870911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203870911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203870911">(Jul 14 2020 at 18:40)</a>:</h4>
<p>Because we already need a fence there</p>



<a name="203871075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203871075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203871075">(Jul 14 2020 at 18:42)</a>:</h4>
<blockquote>
<p>You need to think of the DMA engine as a separate thread. The pointer is not escaping, instead you are sending the pointer to the DMA engine thread.</p>
</blockquote>
<p>Also, what is the difference between pointer escape and sending it to an (unknown) thread ?</p>



<a name="203872649"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203872649" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203872649">(Jul 14 2020 at 18:57)</a>:</h4>
<p>I'd have to check your exact code to be sure.</p>



<a name="203872765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203872765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203872765">(Jul 14 2020 at 18:58)</a>:</h4>
<p>I suggest reading this article which explains acquire and release fences.</p>



<a name="203872775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203872775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203872775">(Jul 14 2020 at 18:58)</a>:</h4>
<p><a href="https://preshing.com/20130922/acquire-and-release-fences/">https://preshing.com/20130922/acquire-and-release-fences/</a></p>



<a name="203873132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203873132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203873132">(Jul 14 2020 at 19:01)</a>:</h4>
<p><span class="user-mention" data-user-id="143274">@Amanieu</span> <br>
<a href="https://github.com/thalesfragoso/stm32-eth/blob/fences/src/tx.rs#L187-L215">https://github.com/thalesfragoso/stm32-eth/blob/fences/src/tx.rs#L187-L215</a></p>



<a name="203873232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203873232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203873232">(Jul 14 2020 at 19:02)</a>:</h4>
<p>One example, the idea is that the writes just need to be done right before enabling the DMA</p>



<a name="203873468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203873468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203873468">(Jul 14 2020 at 19:04)</a>:</h4>
<p>But I'm asking that now because of what you wrote up there about sending the address to the other thread, I guess you said we need a release before sending the address because our read/writes to the buffer could be reordered to after sending the address and that way we would race if the thread is active</p>



<a name="203873528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203873528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203873528">(Jul 14 2020 at 19:04)</a>:</h4>
<p>But in our case the thread is inactive when we send the address, and we have a release fence before activating it</p>



<a name="203873946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203873946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203873946">(Jul 14 2020 at 19:08)</a>:</h4>
<p>That's fine then.</p>



<a name="203874245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203874245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203874245">(Jul 14 2020 at 19:10)</a>:</h4>
<p>Great, thanks</p>



<a name="203922569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203922569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203922569">(Jul 15 2020 at 07:13)</a>:</h4>
<p>yeah, I was actually wondering if your dma descrpitor write really needs to be <code>volatile</code>... in atomic terms, it would no have to be atomic. the only the write to <code>0x40000004</code> would have to be, as that is when things are actually "sent"</p>



<a name="203922594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203922594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203922594">(Jul 15 2020 at 07:14)</a>:</h4>
<p>(the earlier write likely still needs to be volatile for reasons entirely unrelated to synchronization; that's volatile-specific which I am less familiar with)</p>



<a name="203922831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203922831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203922831">(Jul 15 2020 at 07:17)</a>:</h4>
<p>Yes, those are all MMIO accesses, which you usually want to make volatile</p>



<a name="203925873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20I%20rely%20on%20pointer%20escape/capture%20%3F/near/203925873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20I.20rely.20on.20pointer.20escape.2Fcapture.20.3F.html#203925873">(Jul 15 2020 at 07:59)</a>:</h4>
<blockquote>
<p>Also, what is the difference between pointer escape and sending it to an (unknown) thread ?</p>
</blockquote>
<p>For the record, LLVM uses "escape" to mean slightly different things in different optimizations, so there's no precise definition.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>